package com.pangu.mq.rabbitMq.handler;

import com.pangu.exception.MessageException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

/**
 * @author Linbizhao
 * @description: 消息处理工厂
 * @since 2023/11/23 10:24
 */
@Service
public class MessageRabbitMqHandlerFactory {

    private final Map<String, IBusinessRabbitMqHandler> handlerMap = new HashMap<>();

    @Autowired
    public MessageRabbitMqHandlerFactory(Map<String, IBusinessRabbitMqHandler> handlerMap) {
        this.handlerMap.clear();
        handlerMap.forEach(this.handlerMap::put);
    }

    public IBusinessRabbitMqHandler getHandler(String type) {
        return Optional.ofNullable(handlerMap.get(type)).orElseThrow(() -> new MessageException("101", String.format("消息处理器bean:%s 不存在", type)));
    }
}
